﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;

namespace QLST
{
    public partial class frmQuay : Form
    {
        QuayBUS cls = new QuayBUS();
        QuayDTO p = new QuayDTO();
        Boolean flag;
        public frmQuay()
        {
            InitializeComponent();
        }

        private void frmQuay_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            LoadQuay();
        }

        public void LoadQuay()
        {
            gvQuay.DataSource = cls.LoadQuay();
        }
        private void ClearGV()
        {
            gvQuay.ClearSelection();
        }

        private void ClearTextBox()
        {
            txtTen.Text = "";
            txtHienTai.Text = "";
            txtQuiDinh.Text = "";
            txtMaQuay.Text = "";
        }

        private void LockButton(Boolean f)
        {
            btnNVThem.Enabled = f;
            btnNVSua.Enabled = f;
            btnNVXoa.Enabled = f;
            btnNVLuu.Enabled = !f;
            btnNVHuy.Enabled = !f;
        }

        private void LockTextBox(Boolean f)
        {
            txtTen.Enabled = f;
            txtHienTai.Enabled = f;
            txtQuiDinh.Enabled = f;
            txtMaQuay.Enabled = f;
            cmbKho.Enabled = f;
            if (f == false)
            {
                txtTen.BackColor = Color.LemonChiffon;
                txtHienTai.BackColor = Color.LemonChiffon;
                txtQuiDinh.BackColor = Color.LemonChiffon;
                txtMaQuay.BackColor = Color.LemonChiffon;
                cmbKho.BackColor = Color.LemonChiffon;
            }
            else if (f == true)
            {
                txtTen.BackColor = Color.White;
                txtHienTai.BackColor = Color.White;
                txtQuiDinh.BackColor = Color.White;
                txtMaQuay.BackColor = Color.White;
                cmbKho.BackColor = Color.White;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmQuay_Load(sender, e);
        }

        private void gvQuay_SelectionChanged(object sender, EventArgs e)
        {
            txtMaQuay.Text = gvQuay.CurrentRow.Cells["colma"].Value.ToString();
            txtTen.Text = gvQuay.CurrentRow.Cells["colTen"].Value.ToString();
            cmbKho.Text = gvQuay.CurrentRow.Cells["colKho"].Value.ToString();
            txtHienTai.Text = gvQuay.CurrentRow.Cells["col1"].Value.ToString();
            txtQuiDinh.Text = gvQuay.CurrentRow.Cells["col2"].Value.ToString();

        }

        private void btnNVThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvQuay.Enabled = false;
            txtTen.Focus();
            cmbKho.DataSource = cls.Kho();
            cmbKho.DisplayMember = "MaKho";
            cmbKho.ValueMember = "MaKho";
        }

        private void btnNVXoa_Click(object sender, EventArgs e)
        {
            p.MaQuay = txtMaQuay.Text;
            cls.Del_Quay(p);
            frmQuay_Load (sender, e);
        }

        private void btnNVSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvQuay.Enabled = false;
            txtTen.Focus();   
        }

        private void btnNVLuu_Click(object sender, EventArgs e)
        {
            if (txtMaQuay.Text == "")
            {
                flag = true;
            }
            else if (txtMaQuay.Text != "")
            {
                flag = false;
            }
            p.MaKho  = cmbKho .Text ;
            p.TenQuay  = txtTen .Text ;
            p.SoLuongTrenQuay = float.Parse (txtHienTai.Text);
            p.SoLuongQuyDinh = float.Parse(txtQuiDinh.Text);
            cls.IsUp_Quay(p, flag);
            ClearGV();
            frmQuay_Load (sender, e);
            gvQuay.Enabled = true;
            MessageBoxEx.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ClearQuay()
        {
            gvQuay.ClearSelection();
        }

        private void btnNVHuy_Click(object sender, EventArgs e)
        {
            ClearQuay();
            txtTimKiem.Text = "";
            frmQuay_Load (sender, e);
            LockButton(true);
            LockTextBox(false);
            gvQuay.Enabled = true;
        }

        private void btnNVTim_Click(object sender, EventArgs e)
        {
            p.MaQuay = txtTimKiem.Text;
            p.TenQuay = txtTimKiem.Text;
            if (cbMaQuay.Checked == true)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            gvQuay.ClearSelection();
            try
            {
                gvQuay.DataSource = cls.TimQuay(p, flag);
            }
            catch
            {
                MessageBoxEx.Show("Bạn nhập sai Mã Quầy hoặc Tên Quầy", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }                   
        }

       }
}
